<template>
  <div>
    <el-button class="bu_position" size="mini" type="success" @click="toLeague()">返回联赛列表</el-button>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="已有分站赛" name="first">
        <GameList v-if="isFirst" :is-league-author="isLeagueAuthor" :has-final="hasFinal" />
      </el-tab-pane>
      <el-tab-pane :label="isLeagueAuthor ? '待审核分站赛' : '申请的分站赛'" name="second">
        <ApplyList v-if="isSecond" :is-league-author="isLeagueAuthor" :has-final="hasFinal" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import GameList from './gameList';
import ApplyList from './applyList';
import { mapState } from 'vuex';
import {
  api_GET_EventsManagementCompetitionFinalChildrenCountParentId, // 是否已有决赛
  api_GET_EventsManagementCompetitionId // 获取指定赛事
} from '@/extend/api/gameswaggerApi/app';
export default {
  components: {
    GameList,
    ApplyList
  },
  data() {
    return {
      activeName: 'first',
      isFirst: true,
      isSecond: false,
      leagueId: '',
      isLeagueAuthor: '',
      hasFinal: false
    };
  },
  computed: {
    ...mapState({
      tenantId: state => state.user.tenantId
    })
  },
  created() {
    this.leagueId = this.$route.params.parentId || '';
    this.getLeagueData();
    this.getHasFinal();
  },
  methods: {
    // 获取联赛数据
    getLeagueData() {
      var that = this;
      api_GET_EventsManagementCompetitionId(this.leagueId).then(res => {
        const leagueData = res || {};
        that.isLeagueAuthor = leagueData.canEdit;
      }, () => {
        this.$message({ type: 'error', message: '获取联赛数据失败！' });
      });
    },
    // 该联赛是否已有决赛
    getHasFinal() {
      var that = this;
      api_GET_EventsManagementCompetitionFinalChildrenCountParentId(this.leagueId).then(res => {
        that.hasFinal = res === 1;
      });
    },
    handleClick(tab) {
      if (tab.name === 'first') {
        this.isFirst = true;
        this.isSecond = false;
      } else if (tab.name === 'second') {
        this.isFirst = false;
        this.isSecond = true;
      }
    },
    toLeague() {
      const _tenantId = this.tenantId ? this.tenantId : '';
      this.$router.push({
        path: '/t__' + _tenantId + '/game/manage',
        query: { gameType: 'League' }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.bu_position{
  position: absolute;
  right: 15px;
  top: 25px;
  z-index: 10;
}
</style>
